/*
 * @Author: 孙大大
 * @Date: 2022-03-07 09:32:27
 * @LastEditTime: 2022-03-08 11:25:49
 * @FilePath: \toutiao\src\store\index.js
 */
import Vue from "vue";
import Vuex from "vuex";
// 导入封装好的本地存储方法
import { setItem, getItem } from "@/utils/storage";

const TOKEN_KEY = "TOUTIAO_KEY";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    // user: JSON.parse(window.localStorage.getItem(TOKEN_KEY)),
    user: getItem(TOKEN_KEY),
  },
  getters: {},
  mutations: {
    setUser(state, data) {
      state.user = data;
      // 数据保存到本地，以保持持久化
      // window.localStorage.setItem(TOKEN_KEY, JSON.stringify(data));
      setItem(TOKEN_KEY, data);
    },
  },
  actions: {},
  modules: {},
});
